package com.summer.learn.dao;

import com.summer.learn.modle.Department;
import com.summer.learn.modle.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDao {

    private static Map<Integer, Employee> employeeMap;

    static {
        employeeMap = new HashMap<>();
        employeeMap.put(1001, new Employee(1001, "E-AA", "aa@summer.com", 1, new Department(105, "D-EE")));
        employeeMap.put(1002, new Employee(1002, "E-AA", "aa@summer.com", 1, new Department(105, "D-EE")));
        employeeMap.put(1003, new Employee(1003, "E-AA", "aa@summer.com", 1, new Department(105, "D-EE")));
        employeeMap.put(1004, new Employee(1004, "E-AA", "aa@summer.com", 1, new Department(105, "D-EE")));
        employeeMap.put(1005, new Employee(1005, "E-AA", "aa@summer.com", 1, new Department(105, "D-EE")));
    }

    private static Integer initId = 1006;

    @Autowired
    private DepartmentDao departmentDao;

    public void save(Employee employee) {
        if(employee.getId() == null) {
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
        employeeMap.put(employee.getId(), employee);
    }

    public Collection<Employee> getAll() {
        return employeeMap.values();
    }

    public Employee get(int id) {
        return employeeMap.get(id);
    }

    public void delete(int id) {
        employeeMap.remove(id);
    }

}
